#include "dialog.h"
#include<QDebug>
Dialog::Dialog(QWidget *parent)
    : QDialog(parent)
{
    resize(500,300);
    setWindowTitle("QDir类综合控件应用测试");
    lineEdit=new QLineEdit;
//    lineEdit->setText("/");
    listwidget=new QListWidget;
    QVBoxLayout *layout=new QVBoxLayout;

    layout->addWidget(lineEdit);
    layout->addWidget(listwidget);
    this->setLayout(layout);

    QString root="/";
    //定义一个QDir类
    QDir dir(root);

    QStringList slist("*");

    QFileInfoList  slist2=dir.entryInfoList(slist);

    connect(listwidget,&QListWidget::itemDoubleClicked,this,&Dialog::dispdirshow);
    connect(lineEdit,&QLineEdit::returnPressed,this,&Dialog::dispdir);
    //调用此函数来显示
    dispfileinfolist(slist2);


}

Dialog::~Dialog()
{
}

void Dialog::dispfileinfolist(QFileInfoList list)
{
    listwidget->clear();
//    qDebug() << "Number of directories found:" << list.size();
    foreach(QFileInfo info,list)
    {
        if(info.isDir())
        {
            QIcon ico(":/dir.jpg");
//            qDebug()<<ico;
            QString pathname=info.fileName();
//            qDebug() << "Adding directory:" << pathname;
            QListWidgetItem *item=new QListWidgetItem(ico,pathname);
            listwidget->addItem(item);

        }
        else if(info.isFile())
        {
            QIcon ico(":/file.jpg");
//            qDebug()<<ico;
            QString pathname=info.fileName();
//            qDebug() << "Adding directory:" << pathname;
            QListWidgetItem *item=new QListWidgetItem(ico,pathname);
            listwidget->addItem(item);

        }
    }

}

void Dialog::dispdirshow(QListWidgetItem *item)
{
    QString str=item->text();
    QDir dir;
    dir.setPath(lineEdit->text()+QDir::separator()+str);
//    dir.cd(str);

    lineEdit->setText(dir.absolutePath());
    dispdir();
}

void Dialog::dispdir()
{
    QDir dir;
    dir.setPath(lineEdit->text());
    QStringList strlist;
    strlist<<"*";

    QFileInfoList fileinfolist=dir.entryInfoList(strlist,QDir::AllEntries,QDir::DirsFirst);


    dispfileinfolist(fileinfolist);

}

